package com.supermap.business.cloud.tracesource;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.business.cloud.push.MyWebSocket;
import com.supermap.business.push.getui.AppPush;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.cloud.push.Notice;
import com.supermap.dubbo.api.cloud.push.PushService;
import com.supermap.dubbo.api.cloud.tracesource.TraceSourceService;
import com.supermap.dubbo.api.cloud.tracesource.TraceaBility;
import com.supermap.dubbo.api.cloud.tracesource.TraceabilityAudit;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2018年12月21 下午 5:00
 * @email 1301584033@qq.com
 */
@Controller
@RequestMapping("/cloud/tracesource")
public class TraceSourceController {

    @Reference(timeout = 100000)
    private TraceSourceService traceSourceService;

    @Reference(timeout = 100000)
    private PushService pushService;

    /**
     * 预览溯源数据
     * */

    @GetMapping("/getTraceSourceByFarmId")
    @ResponseBody
    public Map<String,Object> getTraceSourceByFarmId(String beginTime, String endTime, String farmId){
        return  traceSourceService.getTraceSourceByFarmId(beginTime,endTime,farmId);
    }


    /**
     * 根据农田查看
     * */
    @GetMapping("/getFarmUserByFarmId")
    @ResponseBody
    public JSONUtil getFarmUserByFarmId(String farmId){
        return  traceSourceService.getFarmUserByFarmId(farmId);
    }


    /**
     * 是否可溯源数据
     * */
    @GetMapping("/isNotHarvestByFarmId")
    @ResponseBody
    public JSONUtil isNotHarvestByFarmId(String farmId){
        return  traceSourceService.isNotHarvestByFarmId(farmId);
    }

    /**
     * 添加溯源
     * */
    @PutMapping("/insertTraceSource")
    @ResponseBody
    public JSONUtil insertTraceSource(@RequestBody TraceaBility traceaBility){
        //审核推送给SMS
        JSONUtil jsonUtil=  traceSourceService.insertTraceSource(traceaBility);
        Notice notice = new Notice();
        notice.setMessageid(jsonUtil.getCountSum());
        notice.setNoticetitle("你有一条溯源信息待审核");
        notice.setNoticetype(9);
        notice.setReceiveuserid(jsonUtil.getMsg());
        notice.setSenduserid((String) jsonUtil.getData());
        pushService.putNotice(notice);
        if(jsonUtil.getCode()==200){
            jsonUtil.setMsg("操作成功!");
            jsonUtil.setData(null);
            jsonUtil.setCountSum(0);
        }else {
            jsonUtil.setMsg("操作失败!");
            jsonUtil.setData(null);
            jsonUtil.setCountSum(0);
        }
        //推送给SMS内部用户
        String noticetype=pushService.getProductNoticeTypes("cqsms2019131id996a15q40ua");
        MyWebSocket.sendInfo(pushService.getReceiveuserIdCount(String.valueOf(notice.getReceiveuserid()),noticetype).getJsonStr(),String.valueOf(notice.getReceiveuserid()));
        return  jsonUtil;
    }

    /**
     * 获取溯源列表
     * */
    @GetMapping("/getTraceSourceList")
    @ResponseBody
    public JSONUtil getTraceSourceList(String farmId,String areaCode,Integer state,Integer userId){
        return  traceSourceService.getTraceSourceList(farmId,areaCode,state,userId);
    }
    /**
     * 分组获取最近一次农田溯源列表
     * */
    @GetMapping("/getTop1TraceSourceList")
    @ResponseBody
    public JSONUtil getTop1TraceSourceList(String areaCode){
        return  traceSourceService.getTop1TraceSourceList(areaCode);
    }

    /**
     * 获取溯源列表
     * */
    @GetMapping("/getAllCropByFarmId")
    @ResponseBody
    public JSONUtil getAllCropByFarmId(String farmId){
        return  traceSourceService.getAllCropByFarmId(farmId);
    }


    /**
     * ------------------------------------修改后溯源-------------------
     * */
    /**
     * 获取用户个人信息
     */
    @GetMapping("/getUserAndFarmByFarmId")
    @ResponseBody
    public JSONUtil getUserAndFarmByFarmId(String farmId){
        return  traceSourceService.getUserAndFarmByFarmId(farmId);
    }


    /**
     * 获取作物生育期
     * */
    @GetMapping("/getTraceSourceCropPubert")
    @ResponseBody
    public JSONUtil getTraceSourceCropPubert(String beginTime, String endTime, String farmId){
        return  traceSourceService.getTraceSourceCropPubert(beginTime,endTime,farmId);
    }


    /**
     * 获取作物生育期详情
     * */
    @GetMapping("/getTraceSourceCropPubertDetails")
    @ResponseBody
    public JSONUtil getTraceSourceCropPubertDetails(String beginTime, String endTime, String farmId){
        return  traceSourceService.getTraceSourceCropPubertDetails(beginTime,endTime,farmId);
    }

    /**
     * 获取农事活动
     * */
    @GetMapping("/getFarming")
    @ResponseBody
    public JSONUtil getFarming(String farmId,String beginTime, String endTime,Integer type){
        return  traceSourceService.getFarming(farmId,beginTime,endTime,type);
    }

    /**
     * 获取默认收获算法时间
     * */
    @GetMapping("/getFarmDateTime")
    @ResponseBody
    public JSONUtil getFarmDateTime(String farmId){
        return  traceSourceService.getFarmDateTime(farmId);
    }

    /**
     * 获取单个审核状态
     * */
    @GetMapping("/getTraceAuth")
    @ResponseBody
    public JSONUtil getTraceAuth(String traceAuthId){
        return  traceSourceService.getTraceAuth(traceAuthId);
    }
    /**
     * 审核
     * */
    @PutMapping("/putTraceAuth")
    @ResponseBody
    public JSONUtil putTraceAuth(@RequestBody  TraceabilityAudit traceabilityAudit){
        //推送全写
        JSONUtil jsonUtil=  traceSourceService.putTraceAuth(traceabilityAudit);
        Notice notice = new Notice();
        notice.setMessageid(jsonUtil.getCountSum());
        notice.setNoticetitle(traceabilityAudit.getSuggest());
        notice.setNoticetype(10);
        notice.setReceiveuserid(String.valueOf(traceSourceService.getUserId(traceabilityAudit.getTratid())));
        notice.setSenduserid(traceabilityAudit.getUserid());
        pushService.putNotice(notice);
        //推送给全国APP外部用户
        String noticetype=pushService.getProductNoticeTypes("quanguoapp2019131xsadwqeq");
        MyWebSocket.sendInfo(pushService.getReceiveuserIdCount(String.valueOf(notice.getReceiveuserid()),noticetype).getJsonStr(),String.valueOf(notice.getReceiveuserid()));
        //个性推送
        List<String> strings=pushService.getUserCode(String.valueOf(notice.getReceiveuserid()));
        AppPush.UserCodePush(strings,"你有一条溯源审核状态",notice.getNoticetitle());
        return  jsonUtil;
    }
    /**
     * 修改
     * */
    @PostMapping("/updateTrace")
    @ResponseBody
    public JSONUtil updateTrace(@RequestBody TraceaBility traceaBility){
        //推送SMS 为写
        JSONUtil jsonUtil=traceSourceService.updateTrace(traceaBility);
        if(traceaBility.getState()!=1) {
            Notice notice = new Notice();
            notice.setMessageid(traceaBility.getId());
            notice.setNoticetitle("你有一条溯源信息待审核");
            notice.setNoticetype(9);
            notice.setReceiveuserid(jsonUtil.getMsg());
            notice.setSenduserid((String) jsonUtil.getData());
            pushService.putNotice(notice);
            //推送给SMS内部用户
            String noticetype = pushService.getProductNoticeTypes("cqsms2019131id996a15q40ua");
            MyWebSocket.sendInfo(pushService.getReceiveuserIdCount(String.valueOf(notice.getReceiveuserid()), noticetype).getJsonStr(), String.valueOf(notice.getReceiveuserid()));

        } return  new JSONUtil();

    }
}
